home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / dm3_src.zip / DMTIME.C < prev    next >
Text File  |  1990-08-02  |  17KB  |  547 lines

  1. /* ************************************************************************* */
  2. /*                                                                           */
  3. /*          D O O R W A R E   D A T E   &   T I M E   L I B R A R Y          */
  4. /*                                                                           */
  5. /*                         For Mycrosoft & Turbo C                           */
  6. /*                                                                           */
  7. /* ************************************************************************* */
  8. /*                                                                           */
  9. /*      This module contains functions used to input and output info         */
  10. /*      with the user and  remote_users console.  Additionally, it           */
  11. /*      contains functions that interface with DOS.                          */
  12. /*                                                                           */
  13. /*      The following funtions are contained in this file:                   */
  14. /*                                                                           */
  15. /*              dmtimer_open    - Access timer hardware                      */
  16. /*              dmtimer_close   - Release timer hardware                     */
  17. /*              dmdelay         - Delay function                             */
  18. /*              dmsleep         - Delay function                             */
  19. /*              get_date        - Get current date in days                   */
  20. /*              cvt_days        - Convert date string to day count           */
  21. /*              get_time        - Get current time in seconds                */
  22. /*              cvt_time        - Convert time string to seconds             */
  23. /*              test_timeout    - Test if user out of time                   */
  24. /*                                                                           */
  25. /*                                                                           */
  26. /* ************************************************************************* */
  27.  
  28.  
  29.  
  30. /* ************************************************************************* */
  31. /*                            PROGRAM HISTORY                                */
  32. /* ************************************************************************* */
  33. /* 06/20/87     Version 1.00                                                 */
  34. /*                                                                           */
  35. /*                                                                           */
  36. /*                                                                           */
  37. /*                                                                           */
  38. /* ************************************************************************* */
  39.  
  40.  
  41.  
  42. #include "dmcfg.h"                              /* Std. defines & includes   */
  43. #include "dmdata.h"                             /* Std DM storage            */
  44.  
  45.  
  46. /*
  47.  *
  48.  * Structure Definitions
  49.  *
  50.  */
  51.  
  52. struct          dmtime
  53. {
  54.    int          type;                   /* Timer type                        */
  55.                                         /* 0 = Multiple Event Interval Timer */
  56.                                         /* 1 = Single Event Countdown Timer  */
  57.    unsigned int counter;                /* Tick Counter                      */
  58.    unsigned int interval;               /* Interval or Countdown Value       */
  59.    unsigned int event;                  /* Event Flag/Counter                */
  60.    char         *link;                  /* Link to next timer                */
  61. };
  62. #define DMTIMER struct dmtime
  63.  
  64.  
  65. /*
  66.  *
  67.  * Local module storage
  68.  *
  69.  */
  70.  
  71. static  int     tm_init_state = 0;
  72. static  int     tm_open_state = 0;
  73. static  int     time_warning  = 1;
  74.  
  75.  
  76. /*
  77.  *
  78.  * Access Timer Hardware
  79.  *
  80.  */
  81.  
  82. dmtimer_open()
  83. {
  84. #if COMPILER == MSC
  85. onexit_t        onexit();                       /* Onexit declaration        */
  86. #endif
  87. #if COMPILER == BTC
  88. int             atexit();
  89. #endif
  90.  
  91. void            dmtimer_close();
  92.  
  93.   if(tm_open_state == 0)
  94.   {
  95.     dmtimer_init();                             /* Access the hardware       */
  96.  
  97.     if(tm_init_state == 0)
  98.     {
  99. #if COMPILER == MSC
  100.       onexit(dmtimer_close);
  101. #endif
  102. #if COMPILER == BTC
  103.       atexit(dmtimer_close);
  104. #endif
  105.       tm_init_state = 1;
  106.     }
  107.     tm_open_state = 1;
  108.   }
  109.   return(0);                                    /* Always returns 0          */
  110. }
  111.  
  112.  
  113. /*
  114.  *
  115.  * Release Timer Hardware
  116.  *
  117.  */
  118.  
  119. void    dmtimer_close()
  120. {
  121.   if(tm_open_state)
  122.   {
  123.     dmtimer_release();                          /* Release the hardware      */
  124.     tm_open_state = 0;
  125.   }
  126.   return;                                       /* Always returns 0          */
  127. }
  128.  
  129.  
  130. /*
  131.  *
  132.  * Sleep n Seconds
  133.  *
  134.  */
  135.  
  136. dmsleep(interval)
  137.  
  138. int     interval;                               /* Time in 1/10th seconds    */
  139. {
  140.    dmdelay(interval * 10);                      /* Execute the delay         */
  141.    return(0);                                   /* All done, no errors       */
  142. }
  143.  
  144.  
  145. /*
  146.  *
  147.  * Sleep n 1/10th Seconds
  148.  *
  149.  */
  150.  
  151. dmdelay(interval)
  152.  
  153. int     interval;                               /* Time in 1/10th seconds    */
  154. {
  155. DMTIMER sleep_timer;                            /* Timer structure           */
  156.  
  157.    modem_wait();                                /* Wait for tx buff empty    */
  158.  
  159.    sleep_timer.type     = 1;                    /* Type is countdown timer   */
  160.    sleep_timer.counter  = interval;             /* Init count                */
  161.    sleep_timer.interval = interval;             /* Init the interval value   */
  162.    sleep_timer.event    = 0;                    /* Flag as no event          */
  163.    dmtimer_add(&sleep_timer);                   /* Add the block to chain    */
  164.  
  165.    while(sleep_timer.event == 0);               /* Until timeout             */
  166.  
  167.    dmtimer_sub(&sleep_timer);                   /* Release resource          */
  168.    return(0);                                   /* All done, no errors       */
  169. }
  170.  
  171.  
  172. /*
  173.  *
  174.  * Get current date
  175.  *
  176.  */
  177.  
  178. get_date(date_string)
  179.  
  180. char    *date_string;
  181. {
  182. long    time();                                 /* Get time function         */
  183. long    cvt_time();                             /* Convert time declaration  */
  184. char    *ctime();                               /* Current time string       */
  185.  
  186. long    time_data;                              /* Current time              */
  187. char    *time_str;                              /* Time string               */
  188.  
  189.    time(&time_data);                            /* Get the current time      */
  190.    time_str = ctime(&time_data);                /* Convert to local time     */
  191.  
  192.    date_string[0]  = time_str[4];               /* Copy month                */
  193.    date_string[1]  = time_str[5];
  194.    date_string[2]  = time_str[6];
  195.    date_string[3]  = time_str[7];
  196.    date_string[4]  = time_str[8];               /* Copy date                 */
  197.    date_string[5]  = time_str[9];
  198.    date_string[6]  = time_str[10];
  199.    date_string[7]  = time_str[20];              /* Copy year                 */
  200.    date_string[8]  = time_str[21];
  201.    date_string[9]  = time_str[22];
  202.    date_string[10] = time_str[23];
  203.    date_string[11] = '\0';                      /* Terminate string          */
  204.  
  205.    return(0);                                   /* Exit, no erros            */
  206. }
  207.  
  208.  
  209. /*
  210.  *
  211.  * Convert date string to days since Jan 1 0000
  212.  *
  213.  */
  214.  
  215. long    cvt_days(date_str)
  216.  
  217. char    *date_str;                              /* Date string               */
  218. {
  219. long    atol();                                 /* Ascii to long             */
  220. long    day_count;                              /* Total days                */
  221. char    mo_str[4];                              /* Month string              */
  222. char    day_str[3];                             /* Day string                */
  223. char    year_str[5];                            /* Year string               */
  224.  
  225.    mo_str[0] = date_str[0];                     /* Copy the month section    */
  226.    mo_str[1] = date_str[1];
  227.    mo_str[2] = date_str[2];
  228.    mo_str[3] = '\0';
  229.  
  230.    day_str[0] = date_str[4];                    /* Copy the minutes section  */
  231.    day_str[1] = date_str[5];
  232.    day_str[2] = '\0';
  233.  
  234.    year_str[0] = date_str[7];                   /* Copy the seconds section  */
  235.    year_str[1] = date_str[8];
  236.    year_str[2] = date_str[9];
  237.    year_str[3] = date_str[10];
  238.    year_str[4] = '\0';
  239.  
  240.    if(strcmp("JAN", strupr(mo_str)) == 0)       /* Jan = month 0             */
  241.      day_count = 0L;
  242.    else if(strcmp("FEB", strupr(mo_str)) == 0)  /* Feb = month 1             */
  243.      day_count = 31L;
  244.    else if(strcmp("MAR", strupr(mo_str)) == 0)  /* Mar = month 2             */
  245.      day_count = 59L;
  246.    else if(strcmp("APR", strupr(mo_str)) == 0)  /* Apr = month 3             */
  247.      day_count = 90L;
  248.    else if(strcmp("MAY", strupr(mo_str)) == 0)  /* May = month 4             */
  249.      day_count = 120L;
  250.    else if(strcmp("JUN", strupr(mo_str)) == 0)  /* Jun = month 5             */
  251.      day_count = 151L;
  252.    else if(strcmp("JUL", strupr(mo_str)) == 0)  /* Jul = month 6             */
  253.      day_count = 181L;
  254.    else if(strcmp("AUG", strupr(mo_str)) == 0)  /* Aug = month 7             */
  255.      day_count = 212L;
  256.    else if(strcmp("SEP", strupr(mo_str)) == 0)  /* Sep = month 8             */
  257.      day_count = 243L;
  258.    else if(strcmp("OCT", strupr(mo_str)) == 0)  /* Oct = month 9             */
  259.      day_count = 273L;
  260.    else if(strcmp("NOV", strupr(mo_str)) == 0)  /* Nov = month 10            */
  261.      day_count = 304L;
  262.    else if(strcmp("DEC", strupr(mo_str)) == 0)  /* Dec = month 11            */
  263.      day_count = 334L;
  264.  
  265.    day_count  += atol(day_str) * 1L;
  266.    day_count  += atol(year_str) * 365L;
  267.  
  268.    return(day_count);                           /* Exit, with count in days  */
  269. }
  270.  
  271.  
  272. /*
  273.  *
  274.  * Get number of seconds past midnight
  275.  *
  276.  */
  277.  
  278. long    cur_time()
  279. {
  280. long    time();                                 /* Get time function         */
  281. long    cvt_time();                             /* Convert time declaration  */
  282. char    *ctime();                               /* Current time string       */
  283.  
  284. long    time_data;                              /* Current time              */
  285. char    *time_str;                              /* Time string               */
  286.  
  287.    time(&time_data);                            /* Get the current time      */
  288.    time_str = ctime(&time_data);                /* Convert to local time     */
  289.  
  290.    time_data = cvt_time(&time_str[11]);         /* Convert to seconds        */
  291.    return(time_data);                           /* Exit, with time in secs.  */
  292. }
  293.  
  294.  
  295. /*
  296.  *
  297.  * Convert time string to seconds past midnight
  298.  *
  299.  */
  300.  
  301. long    cvt_time(time_str)
  302.  
  303. char    *time_str;                              /* Time string               */
  304. {
  305. long    atol();                                 /* Ascii to long             */
  306. long    time_data;                              /* Current time              */
  307. char    hour_str[3];                            /* Hours string              */
  308. char    minu_str[3];                            /* Minutes string            */
  309. char    seco_str[3];                            /* Seconds string            */
  310.  
  311.    hour_str[0] = time_str[0];                   /* Copy the hours section    */
  312.    hour_str[1] = time_str[1];
  313.    hour_str[2] = '\0';
  314.  
  315.    minu_str[0] = time_str[3];                   /* Copy the minutes section  */
  316.    minu_str[1] = time_str[4];
  317.    minu_str[2] = '\0';
  318.  
  319.    seco_str[0] = time_str[6];                   /* Copy the seconds section  */
  320.    seco_str[1] = time_str[7];
  321.    seco_str[2] = '\0';
  322.  
  323.    time_data   = atol(hour_str) * 3600L;        /* Convert to seconds        */
  324.    time_data  += atol(minu_str) * 60L;
  325.    time_data  += atol(seco_str) * 1L;
  326.  
  327.    return(time_data);                           /* Exit, with time in secs.  */
  328. }
  329.  
  330.  
  331. /*
  332.  *
  333.  * Test for timeout condition
  334.  *
  335.  */
  336.  
  337. test_timeout()
  338. {
  339. long    cur_time();                             /* Get current time decl.    */
  340. long    cctime;                                 /* Current_time              */
  341.  
  342.    if(remote_user)
  343.    {
  344.      cctime = cur_time();                       /* Get the time              */
  345.      if(cctime < user_start)                    /* If we wrapped past 12...  */
  346.        cctime += DAY_SECONDS;                   /* ...adjust it              */
  347.  
  348.      if(cctime > user_signoff)                  /* If time used up...        */
  349.        return(-2);                              /* ...alert caller           */
  350.  
  351.      if(time_warning)                           /* If we are checking...     */
  352.      {
  353.        if(cctime > (user_signoff - 300L))       /* If less than 5 minutes... */
  354.        {
  355.          time_warning = 0;                      /* ...clip the flag          */
  356.          return(-1);                            /* ...alert caller           */
  357.        }
  358.      }
  359.    }
  360.    return(0);                                   /* Exit, with no error       */
  361. }
  362.  
  363.  
  364. /*
  365.  *
  366.  * Load time control file & test controls
  367.  *
  368.  */
  369.  
  370. time_controls(timefile)
  371.  
  372. char    *timefile;
  373. {
  374. FS      rfd;
  375.  
  376. long    cvt_time();
  377. long    end_time;
  378. int     i, j, k;
  379.  
  380.    /*
  381.     * Sysop default hours
  382.     */
  383.  
  384.    bbs_time_info.sysop_start = cvt_time("00:00:00");
  385.    bbs_time_info.sysop_stop  = cvt_time("23:59:59");
  386.  
  387.    user_access_level = -1;                      /* assume not used           */
  388.  
  389.  
  390.    /*
  391.     * Open up the time file
  392.     */
  393.  
  394.    strcpy(rfd.name, timefile);
  395.    i = file_open(&rfd, FREAD, FBINARY, FNOCREATE); /* Open time file         */
  396.    if(i)                                        /* If it does not exist      */
  397.      return(0);                                 /* ...exit with error        */
  398.  
  399.    /*
  400.     * Process file info
  401.     */
  402.  
  403.    if(fread(&bbs_time_info, 1, sizeof(struct bbs_time), rfd.fd) !=
  404.       sizeof(struct bbs_time))                  /* If bad read...            */
  405.    {
  406.      file_close(&rfd);                           /* ...close file             */
  407.      return(-1);                                /* ... exit with error       */
  408.    }
  409.    file_close(&rfd);
  410.  
  411.  
  412.    /*
  413.     * Test if security high enough
  414.     */
  415.  
  416.    i = -1;
  417.    j = 0;
  418.    while((i < 7) && (j == 0))
  419.    {
  420.      if(user_security >= bbs_time_info.levels[i + 1])
  421.        i++;
  422.      else
  423.        j = 1;
  424.    }
  425.    if(i == -1)                                  /* error if too low of secur.*/
  426.      return(-2);
  427.  
  428.    user_access_level = i;                       /* Store access level        */
  429.  
  430.  
  431.    /*
  432.     * Adjust time limit
  433.     */
  434.  
  435.    j = 0;
  436.    while((j < 7) && (user_start >= bbs_time_info.access[j + 1].start_time) &&
  437.                     (bbs_time_info.access[j + 1].start_time != 0L))
  438.    {
  439.      j++;
  440.    }
  441.  
  442.    if(bbs_time_info.access[j].levels[i] == 0L)
  443.      return(-3);                                /* Not allowed now           */
  444.  
  445.    end_time  = bbs_time_info.access[j].levels[i] * 60L;
  446.    end_time += user_start;
  447.  
  448.    if(end_time < user_signoff)
  449.      user_signoff = end_time;
  450.  
  451.    return(1);
  452. }
  453.  
  454.  
  455. /*
  456.  *
  457.  * Test daily time controls
  458.  *
  459.  */
  460.  
  461. daily_controls()
  462. {
  463. long    cur_time();
  464. long    cvt_days();
  465.  
  466. long    today_date;
  467. long    last_date;
  468. long    now_time;
  469. long    end_time;
  470. int     i, j, k;
  471. char    date[12];
  472.  
  473.    /*
  474.     * Test if controls are active
  475.     */
  476.  
  477.    if(user_access_level >= 0)
  478.    {
  479.      /*
  480.       * Test if user played today
  481.       */
  482.  
  483.      get_date(date);
  484.      today_date = cvt_days(date);
  485.      last_date  = cvt_days(user_game_date);
  486.  
  487.      if(today_date != last_date)
  488.      {
  489.        get_date(user_game_date);                /* set today as last date    */
  490.        user_day_time  = 0L;
  491.        user_day_games = 0;
  492.        return(0);                               /* exit all ok               */
  493.      }
  494.  
  495.  
  496.      /*
  497.       * Test if user waited long enough since last game
  498.       */
  499.  
  500.      if(bbs_time_info.wait[user_access_level] != 0)
  501.      {
  502.        now_time = cur_time();
  503.        end_time = user_game_end + (bbs_time_info.wait[user_access_level] * 60L);
  504.  
  505.        if(now_time < end_time)
  506.          return(-1);                            /* error wait longer         */
  507.      }
  508.  
  509.  
  510.      /*
  511.       * Test if user played too many games today
  512.       */
  513.  
  514.      if(bbs_time_info.dayg[user_access_level] != 0)
  515.      {
  516.        if(user_day_games >= bbs_time_info.dayg[user_access_level])
  517.          return(-2);                            /* error too many games      */
  518.      }
  519.  
  520.  
  521.      /*
  522.       * Test if user played too many minutes today
  523.       */
  524.  
  525.      if(bbs_time_info.dayt[user_access_level] != 0)
  526.      {
  527.        if(user_day_time >= (bbs_time_info.dayt[user_access_level] * 60L))
  528.          return(-3);                            /* error no more time today  */
  529.  
  530.  
  531.        /*
  532.         * Adjust time limit based on daily rate
  533.         */
  534.  
  535.        end_time  = (bbs_time_info.dayt[user_access_level] * 60L) - user_day_time;
  536.        end_time += user_start;
  537.  
  538.        if(end_time < user_signoff)
  539.          user_signoff = end_time;
  540.  
  541.      }
  542.    }
  543.    return(0);
  544. }
  545.  
  546.  
  547.